<template>
  <div class="index">
    <div class="title">
      在线网盘系统
    </div>
    <div class="label">
      Cloud Driver
    </div>
    <div class="btn">
      <el-button type="primary"
        @click="dialogFormVisible = true">登录/注册</el-button>
    </div>
    <el-dialog title="登录/注册"
      :visible.sync="dialogFormVisible"
      width="400px">
      <el-form :model="form">
        <el-form-item label="用户名"
          :label-width="formLabelWidth">
          <el-input v-model="form.username"
            autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码"
          :label-width="formLabelWidth">
          <el-input v-model="form.password"
            autocomplete="off"
            type="password"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer"
        class="dialog-footer">
        <el-button @click="signUp" :disabled="loningIf">注 册</el-button>
        <el-button type="primary"
          @click="signIn" :disabled="loningIf">登 录</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'index',
  data () {
    return {
      dialogFormVisible: false,
      form: {
        username: '',
        password: ''
      },
      formLabelWidth: '120px',
      loningIf: false
    }
  },
  methods: {
    // 登录
    signIn () {
      // 加载弹框
      this.openLoading()
      this.loningIf = true
      this.dialogFormVisible = false
      this.$http
        .post('/user/validate', this.form)
        .then(res => {
          if (res.data.flag === 0) {
            this.$message.error(res.data.msg)
          } else {
            // console.log(res)
            this.$message.success(res.data.msg)
            sessionStorage.setItem('uid', res.data.data.uid)
            sessionStorage.setItem('username', res.data.data.username)
            // 关闭加载弹框
            this.closeLoading()
            this.loningIf = false
            //  跳转到其他页面
            this.$router.push('tab-list')
          }
          // 关闭加载弹框
          this.closeLoading()
          this.loningIf = false
        })
        .catch(err => {
          console.log(err)
          // 关闭加载弹框
          this.closeLoading()
          this.loningIf = false
        })
    },
    //  注册
    signUp () {
      // 加载弹框
      this.openLoading()
      // 按钮禁用
      this.loningIf = true
      this.dialogFormVisible = false
      this.$http
        .post('/user/add', this.form)
        .then(res => {
          if (res.data.flag === 0) {
            this.$message({
              showClose: true,
              message: '用户名或密码不能为空',
              type: 'warning'
            })
            // 关闭加载弹框
            this.closeLoading()
            this.loningIf = false
          } else {
            this.$message.success(res.data.msg)
            // 关闭加载弹框
            this.closeLoading()
            this.loningIf = false
            this.$router.push('tab-list')
          }
        })
        .catch(err => {
          // 关闭加载弹框
          this.closeLoading()
          console.log(err)
          this.loningIf = false
        })
    },
    // 加载
    openLoading () {
      this.loading = this.$loading({
        lock: true,
        text: '正在加载',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
    },
    // 取消加载
    closeLoading () {
      this.loading.close()
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.index {
  margin-top: 80px;
  text-align: center;
}
.title {
  font-size: 60px;
}
.label {
  font-size: 40px;
  margin-top: 10px;
}
.btn {
  margin-top: 10px;
}
.dialog-footer {
  text-align: center;
}
</style>
